package com.hqhsc.module.manage.activity.dao;

import com.hqhsc.core.dao.BaseJdbcDaoImpl;
import com.hqhsc.module.common.dao.CommonDao;
import com.hqhsc.module.manage.activity.domain.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Repository
public class ActivityManageDao extends BaseJdbcDaoImpl {
	final Logger logger = LoggerFactory.getLogger(ActivityManageDao.class);

    @Autowired
    private CommonDao commonDao;

    public long getActivityCount(long org_id , String activity_name , int activity_type) {
        String sql = "select count(1) from activity t where 1=1 ";
        List<Object> list = new ArrayList<Object>();
        if(StringUtils.isNotEmpty(activity_name)) {
            sql = sql + " and name like ? ";
            list.add("%"+activity_name+"%");
        }
        if(activity_type != -1) {
            sql = sql + " and type = ? ";
            list.add(activity_type);
        }
        Object[] o = list.toArray();
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().queryForLong(sql, o);
    }


    public List<Activity> getActivityList(long org_id , String activity_name , int activity_type , String orderby , String orderby_type , int start ,  int size) {

        String sql = "select t.* , a.user_name from activity t , users a where t.create_by = a.user_id ";
        List<Object> list = new ArrayList<Object>();
        if(StringUtils.isNotEmpty(activity_name)) {
            sql = sql + " and name like ? ";
            list.add("%"+activity_name+"%");
        }
        if(activity_type != -1) {//-1表示查所有；0表示线下；1表示线上
            sql = sql + " and type = ? ";
            list.add(activity_type);
        }
        if(StringUtils.isNotEmpty(orderby)) {
            sql = sql + " order by " + orderby + " " + orderby_type;
        } else {
            sql = sql + " order by id desc ";
        }
        sql = sql + " limit ? ,? ";
        list.add(start);
        list.add(size);
        Object[] o = list.toArray();
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().query(sql, o, new ActivityRowMapper());
    }

    public int getActivityTempletCount() {
        String sql = "select count(1) from activity_templet where status = 1";
        Object[] o = new Object[]{};
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().queryForInt(sql, o);
    }

    public List<ActivityTemplet> getActivityTempletList(int start , int size) {

        String sql = "select * from activity_templet where status = 1 order by id desc limit ?,?";
        Object[] o = new Object[]{ start , size};
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().query(sql, o, new ActivityTempletRowMapper());
    }

    public int insertActivity(long activity_id , String activity_name , int templet_id , int type , String title , String desc , String content , String flow , String place , Date begin_time , Date end_time , String file_path_db , String sign_qr_code , long userid) {
        String sql = "insert into activity (id , name , templet_id , type , title , description , content , flow , place , begin_time , end_time , cover_url , sign_qr_code , create_by) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] o = new Object[] { activity_id , activity_name , templet_id , type , title , desc , content , flow , place , begin_time , end_time , file_path_db , sign_qr_code , userid };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql, o);
    }

    public void batchUpdateChannelActivityNum(final String[] channel_id_arg , final int incr) {
        String sql = "update channel set activity_num = activity_num + ? where id = ?";
        try {
            this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ps.setLong(1, incr);
                    ps.setLong(2, Long.valueOf(channel_id_arg[i]));
                }
                public int getBatchSize() {
                    return channel_id_arg.length;
                }
            });
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void batchInsertChannelActivity(final String[] channel_id_arg , final long activity_id) {
        String sql = "insert into activity_channel_detail ( channel_id , activity_id) values (?,?)";
        try {
            this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
//                    ps.setLong(1, batch_params.get(i).get("channel_activity_id"));
                    ps.setLong(1, Long.valueOf(channel_id_arg[i]));
                    ps.setLong(2, activity_id);
                }
                public int getBatchSize() {
                    return channel_id_arg.length;
                }
            });
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    public List<ActivityChannelDetail> getActivityChannelDetailByActivityId(long activity_id) {
        String sql = "select t.* , a.name , a.spread_cost from activity_channel_detail t , channel a where t.activity_id = ? and t.channel_id =  a.id ";
        Object[] o = new Object[] { activity_id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().query(sql, o, new ActivityChannelDetailRowMapper());
    }

    public int updateActivityChannelQrCode(long id , String qr_code) {
        String sql = "update activity_channel_detail t set t.qr_code = ? where id = ?";
        Object[] o = new Object[] { qr_code , id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql , o);
    }

    public int updateActivityStatus(long activity_id , int desc_status) {
        String sql = "update activity t set status = ? where id = ?";
        Object[] o = new Object[] { desc_status , activity_id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql , o);
    }


    public int updateActivity(long activity_id , String activity_name , int type , String title , String desc , String content , String flow , String place , Date begin_time , Date end_time) {
        String sql = "update activity set  name = ? , type = ? , title = ? , description = ? , content = ? , " +
                "flow = ? , place = ? , begin_time = ? , end_time = ? where id = ?";
        Object[] o = new Object[] { activity_name , type , title , desc , content ,
                flow , place , begin_time , end_time , activity_id  };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql, o);
    }

    public int deleteActivityChannelByChannelId(long activity_id) {
        String sql = "delete from activity_channel_detail where activity_id = ?";
        Object[] o = new Object[] { activity_id  };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql, o);
    }

}
